<?php
namespace Tbf\Bundle\CronBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
 * 给事件dispatcher和事件subscriber上tag管理
 */
class PersistCronCompilerPass implements CompilerPassInterface
{
    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     *
     * @api
     */
    public function process(ContainerBuilder $container)
    {
        if (!$container->hasDefinition('tbf_cron')) {
            return;
        }
        $cron_definition = $container->getDefinition('tbf_cron');
        $persist_cron_list = $container->findTaggedServiceIds(
            'tbf_cron.persist_cron'
        );
        foreach ($persist_cron_list as $persist_cron_id=>$persist_cron_attributes) {
            $cron_definition->addMethodCall(
                    'addPersistCron',
                    array(new Reference($persist_cron_id))
                );
        }
    }
}
